package com.example.recordingpen_datacollection.Utils.HtmlLayoutUtil;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class BDHtmlLayoutV2Util {
    /**
     * 将文本内容转换为HTML格式
     * @param text 文本内容
     * @return 格式化后的HTML字符串
     */
    public String convertToHtml(String text) {
        boolean flat = true;
        text = text.replaceAll(" ", "&nbsp;");
        text = text.replaceAll("\\\\n", "\n");
        // 这里我们简单地将文本内容包装在HTML的<p>标签中
        // 你可以根据需要添加更多的HTML结构和样式
        StringBuilder htmlBuilder = new StringBuilder();
        htmlBuilder.append("<!DOCTYPE html>\n");
        htmlBuilder.append("<html lang=\"zh-CN\">\n");
        htmlBuilder.append("<head>\n");
        htmlBuilder.append("<meta charset=\"UTF-8\">\n");
        htmlBuilder.append("<title>会议内容记录</title>\n");
        // 可以在这里添加CSS样式，如果需要的话
        htmlBuilder.append("</head>\n");
        htmlBuilder.append("<body>\n");

        // 假设文本内容已经按照你的需要分割成了各个部分
        // 这里我们简单地用\n\n来模拟段落分隔
        String[] paragraphs = text.split("\n");
        for (String paragraph : paragraphs) {

//            System.out.println("paragraph: "+paragraph);
            if(paragraph.length()>4){
                String substring = paragraph.substring(0, 4);
//                System.out.println("substring: "+substring);
                if(substring.equals("会议主题") || substring.equals("會議主題")){
                    htmlBuilder.append("<h1>").append(paragraph.trim()).append("</h1><hr>\n");
                    continue;
                } else if(substring.equals("会议摘要")|| substring.equals("會議摘要")){

                    {
                        boolean parag = hasDoubleReturnAfterMeetingSummary(paragraph, "会议摘要", "\n");
                        if(!parag){

                            modifyingHtmlFormat(paragraph, substring, "\n",htmlBuilder);
                            continue;
                        }
                    }
                    htmlBuilder.append("<h2>").append(paragraph.trim()).append("</h2>\n");
                    continue;
                } else if(substring.equals("会议大纲")|| substring.equals("會議大綱")){
                    {
                        boolean parag = hasDoubleReturnAfterMeetingSummary(paragraph, "会议大纲", "\n");
                        if(!parag){

                            modifyingHtmlFormat(paragraph, substring, "\n",htmlBuilder);
                            continue;
                        }
                    }
                    htmlBuilder.append("<h2>").append(paragraph.trim()).append("</h2>\n");
                    continue;
                }else if(substring.equals("会议总结")|| substring.equals("會議總結")){
                    {
                        boolean parag = hasDoubleReturnAfterMeetingSummary(paragraph, "会议总结", "\n");
                        if(!parag){

                            modifyingHtmlFormat(paragraph, substring, "\n",htmlBuilder);
                            continue;
                        }
                    }
                    htmlBuilder.append("<h2>").append(paragraph.trim()).append("</h2>\n");
                    continue;
                }
                String regex = "([一二三四五六七八九十]+、)(?!\\\\n)";
                if (substring.matches(".*(" + regex + ").*")) {
                    htmlBuilder.append("<b>").append(paragraph.trim()).append("</b></br>\n");
                    flat = false;
                    continue;
                }

                Pattern pattern = Pattern.compile("(\\d+\\.)"); // 匹配一个或多个数字后跟一个点
                Matcher matcher = pattern.matcher(substring);
                Pattern pattern2 = Pattern.compile("(\\d+\\.\\d)"); // 匹配一个或多个数字后跟一个点
                Matcher matcher2 = pattern2.matcher(substring);
                if ((matcher.find() && flat) && (!matcher2.find())) { // 如果找到匹配项
                    String prefix = matcher.group(); // 获取匹配的子串（即前面的数字和点）

                    htmlBuilder.append("<b>").append(paragraph.trim()).append("</b></br>\n");
                    continue;
                }
            }
            if(!paragraph.equals("")){
                // 处理每个段落，比如将每个段落包装在<p>标签中
                htmlBuilder.append("<p>").append(paragraph.trim()).append("</p>\n");
            }

        }

        // 可以在这里添加更多的HTML内容，比如列表、表格等

        htmlBuilder.append("</body>\n");
        htmlBuilder.append("</html>");

        return htmlBuilder.toString();
    }


    private static boolean hasDoubleReturnAfterMeetingSummary(String text, String prefix, String doubleReturn) {
        int prefixIndex = text.indexOf(prefix);
        if (prefixIndex != -1) {
            // 跳过前缀的长度
            int checkIndex = prefixIndex + prefix.length()+1;
            // 确保我们不会越界
            if (checkIndex + doubleReturn.length() <= text.length()) {
                // 检查接下来的字符是否与doubleReturn匹配
                return text.substring(checkIndex, checkIndex + doubleReturn.length()).equals(doubleReturn);
            }else {
                return true;
            }
        }
        // 如果前缀未找到或越界，则返回false
        return false;
    }

    //修改html格式
    private static void modifyingHtmlFormat(String paragraph, String txt, String sa, StringBuilder htmlBuilder){

        StringBuilder sb = new StringBuilder(paragraph);
        int position = 5; // 假设要在"会议摘要"后面插入一个字符
        char toInsert = '\n';
        if (position < sb.length()) {
            sb.insert(position, toInsert);
            String text2 = sb.toString();
            String[] paragraphs2 = text2.split("\n");
            if(paragraphs2.length>=2){
                htmlBuilder.append("<h2>").append(paragraphs2[0].trim()).append("</h2>\n");
                htmlBuilder.append("<p>").append("&nbsp;&nbsp;&nbsp;&nbsp"+paragraphs2[1].trim()).append("</p>\n");
            } else {
                htmlBuilder.append("<h2>").append(paragraphs2[0].trim()).append("</h2>\n");
            }

        } else {
            System.out.println("插入位置超出字符串长度。");
        }
    }



    public String convertToTXT(String str2){
        String formattedStr = str2.replaceAll("會議主題：", "會議主題：\n")
                .replaceAll("会议主题：", "会议主题：\n")
                .replaceAll("會議主題:", "會議主題:\n")
                .replaceAll("会议主题:", "会议主题:\n")
                .replaceAll("會議摘要：", "\n會議摘要：\n")
                .replaceAll("会议摘要：", "\n会议摘要：\n")
                .replaceAll("會議摘要:", "\n會議摘要:\n")
                .replaceAll("会议摘要:", "\n会议摘要:\n")
                .replaceAll("會議大綱：", "\n會議大綱：")
                .replaceAll("会议大纲：", "\n会议大纲：")
                .replaceAll("會議大綱:", "\n會議大綱:")
                .replaceAll("会议大纲:", "\n会议大纲:")
                .replaceAll("會議總結：", "\n會議總結：")
                .replaceAll("会议总结：", "\n会议总结：")
                .replaceAll("會議總結:", "\n會議總結:")
                .replaceAll("会议总结:", "\n会议总结:")
                .replaceAll("([一二三四五六七八九十]+、)(?!\\\\n)", "\n$1")
                .replaceAll("(\\d+\\.\\s)", "\n$1") // 在数字加点的部分前添加换行符，假设这是子点的开始
                .replaceAll("(\\d+\\.\\d\\s)", "\n$1")
                // 可能还需要添加其他规则来处理其他情况
                ;
        return formattedStr.replaceAll("\n\n","\n");
    }
}
